package org.dromara.third.convert.personLocationData.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.PersonLocationData;
import org.dromara.system.mapper.PersonLocationDataMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.personLocationData.dto.PersonLocationDataDTO;
import org.dromara.third.convert.personLocationData.validator.PersonLocationDataValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertPersonLocationDataController {

    @Autowired
    private PersonLocationDataMapper personLocationDataMapper;

    @PostMapping("/personLocationData")
    @Transactional
    public R<Void> addPersonLocationData(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        PersonLocationDataDTO personLocationDataDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            personLocationDataDTO = JSONObject.parseObject(decrypt, PersonLocationDataDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, personLocationDataDTO, new PersonLocationDataValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        PersonLocationData personLocationData = assemblePersonLocationData(orgCode, personLocationDataDTO);
        int count = personLocationDataMapper.countWithOutDelFlag(personLocationData.getId());
        if (count == 0) {
            personLocationDataMapper.insert(personLocationData);
        } else {
            personLocationDataMapper.updateWithOutDelFlag(personLocationData);
        }
        return R.ok();
    }

    private PersonLocationData assemblePersonLocationData(String orgCode, PersonLocationDataDTO dto) {
        PersonLocationData personLocationData = new PersonLocationData();
        try {
            personLocationData.setId(dto.getId());
            personLocationData.setCardId(dto.getCardId());
            personLocationData.setName(dto.getName());
            personLocationData.setLongitude(dto.getLongitude());
            personLocationData.setLatitude(dto.getLatitude());
            personLocationData.setDelFlag(Long.parseLong(dto.getDelFlag()));
            personLocationData.setCreateDept(Long.parseLong(dto.getCreateDept()));
            personLocationData.setCreateBy(Long.parseLong(dto.getCreateBy()));
            personLocationData.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            personLocationData.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            personLocationData.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            personLocationData.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装人员定位信息异常");
        }
        return personLocationData;
    }
}
